﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Web.UI.DataVisualization.Charting;

namespace waGerencial
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Gráfico atividades PA e Lista

            // Populate series data
            double[] yValuesChartAtivPALista = { 70.62, 30 };
            string[] xValuesChartAtivPALista = { "PA", "Lista" };
            
            chartAtivPALista.Titles.Add("PA x Lista");

            chartAtivPALista.Series["Default"].Points.DataBindXY(xValuesChartAtivPALista, yValuesChartAtivPALista);
            // Set Doughnut chart type
            chartAtivPALista.Series["Default"].ChartType = SeriesChartType.Pie;
            // Set labels style
            chartAtivPALista.Series["Default"]["PieLabelStyle"] = "Inside";
            // Set Doughnut radius percentage
            chartAtivPALista.Series["Default"]["DoughnutRadius"] = "30";
            // Enable 3D
            chartAtivPALista.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;
            // Disable the Legend
            chartAtivPALista.Legends[0].Enabled = false;

            //Gráfico atividades Planejadas e Não Planejadas
            
            // Populate series data
            double[] yValuesChartAtivPlanej = { 80.00, 20.00 };
            string[] xValuesChartAtivPlanej = { "Planej.", "Não Planej." };

            chartAtivPlanej.Titles.Add("Planejadas x Não Planejadas");

            chartAtivPlanej.Series["Default"].Points.DataBindXY(xValuesChartAtivPlanej, yValuesChartAtivPlanej);
            // Set Doughnut chart type
            chartAtivPlanej.Series["Default"].ChartType = SeriesChartType.Pie;
            // Set labels style
            chartAtivPlanej.Series["Default"]["PieLabelStyle"] = "Inside";
            // Set Doughnut radius percentage
            chartAtivPlanej.Series["Default"]["DoughnutRadius"] = "30";
            // Enable 3D
            chartAtivPlanej.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;
            // Disable the Legend
            chartAtivPlanej.Legends[0].Enabled = false;

            //Gráfico Tipos de Pendência
            
            // Populate series data
            double[] yValuesChartTipoPendencia = { 80, 20, 30 };
            string[] xValuesChartTipoPendencia = { "Bug", "Melhoria", "Nova Funcionalidade" };

            chartTipoPendencia.Titles.Add("Por tipo");

            chartTipoPendencia.Series["Default"].Points.DataBindXY(xValuesChartTipoPendencia, yValuesChartTipoPendencia);
            // Set Doughnut chart type
            chartTipoPendencia.Series["Default"].ChartType = SeriesChartType.Pie;
            // Set labels style
            chartTipoPendencia.Series["Default"]["PieLabelStyle"] = "Inside";
            // Set Doughnut radius percentage
            chartTipoPendencia.Series["Default"]["DoughnutRadius"] = "30";
            // Enable 3D
            chartTipoPendencia.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;
            // Disable the Legend
            chartTipoPendencia.Legends[0].Enabled = false;

            //Gráfico Revisões Origem
            
            // Populate series data
            double[] yValuesChartOrigemRevisao = { 40, 30};
            string[] xValuesChartOrigemRevisao = { "Teste", "Homologação" };

            chartOrigemRevisao.Titles.Add("Origem");

            chartOrigemRevisao.Series["Default"].Points.DataBindXY(xValuesChartOrigemRevisao, yValuesChartOrigemRevisao);
            // Set Doughnut chart type
            chartOrigemRevisao.Series["Default"].ChartType = SeriesChartType.Pie;
            // Set labels style
            chartOrigemRevisao.Series["Default"]["PieLabelStyle"] = "Inside";
            // Set Doughnut radius percentage
            chartOrigemRevisao.Series["Default"]["DoughnutRadius"] = "30";
            // Enable 3D
            chartOrigemRevisao.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;
            // Disable the Legend
            chartOrigemRevisao.Legends[0].Enabled = false;

            //Gráfico Tempo Gasto por Etapa
            
            // Populate series data
            double[] yValuesChartDistrEtapa = { 40, 30, 60, 10 };
            string[] xValuesChartDistrEtapa = { "Análise Técnica", "Desenvolvimento", "Teste", "Revisão" };

            chartDistrEtapa.Titles.Add("Gasto por Etapa");

            chartDistrEtapa.Series["Default"].Points.DataBindXY(xValuesChartDistrEtapa, yValuesChartDistrEtapa);
            // Set Doughnut chart type
            chartDistrEtapa.Series["Default"].ChartType = SeriesChartType.Pie;
            // Set labels style
            chartDistrEtapa.Series["Default"]["PieLabelStyle"] = "Inside";
            // Set Doughnut radius percentage
            chartDistrEtapa.Series["Default"]["DoughnutRadius"] = "30";
            // Enable 3D
            chartDistrEtapa.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;
            // Disable the Legend
            chartDistrEtapa.Legends[0].Enabled = false;


            //Gráfico Tempo Estimado x Gasto

            chartEstimadoGasto.Titles.Add("Estimado x Gasto");

            // Populate series data
            double[] yValuesChartEstimadoGastoEst = { 40, 30};
            string[] xValuesChartEstimadoGastoEst = { "Desenvolvimento", "Teste"};
            chartEstimadoGasto.Series["Estimado"].Points.DataBindXY(xValuesChartEstimadoGastoEst, yValuesChartEstimadoGastoEst);

            // Set series chart type
            chartEstimadoGasto.Series["Estimado"].ChartType = SeriesChartType.Column;
            // Set series point width
            chartEstimadoGasto.Series["Estimado"]["PointWidth"] = "0.6";
            // Show data points labels
            //chartEstimadoGasto.Series["Estimado"].IsValueShownAsLabel = true;
            // Set data points label style
            chartEstimadoGasto.Series["Estimado"]["BarLabelStyle"] = "Center";
            // Show as 3D
            //chartEstimadoGasto.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;
            // Draw as 3D Cylinder
            chartEstimadoGasto.Series["Estimado"]["DrawingStyle"] = "Emboss";

            double[] yValuesChartEstimadoGastoGasto = { 50, 30 };
            string[] xValuesChartEstimadoGastoGasto = { "Desenvolvimento", "Teste" };
            chartEstimadoGasto.Series["Gasto"].Points.DataBindXY(xValuesChartEstimadoGastoGasto, yValuesChartEstimadoGastoGasto);

            // Set series chart type
            chartEstimadoGasto.Series["Gasto"].ChartType = SeriesChartType.Column;
            // Set series point width
            chartEstimadoGasto.Series["Gasto"]["PointWidth"] = "0.6";
            // Show data points labels
            //chartEstimadoGasto.Series["Gasto"].IsValueShownAsLabel = true;
            // Set data points label style
            chartEstimadoGasto.Series["Gasto"]["BarLabelStyle"] = "Center";
            // Show as 3D
            //chartEstimadoGasto.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;
            // Draw as 3D Cylinder
            chartEstimadoGasto.Series["Gasto"]["DrawingStyle"] = "Emboss";
            // Disable the Legend
            //chartEstimadoGasto.Legends[0].Enabled = false;
        }

        private double segundosToHoras(int vpSegundos)
        {
            double result = Convert.ToDouble(vpSegundos/3600);
            return result;
        }

        private int[] totaliza(String vpEnderecoXML)
        {
            int totalTimeEstimate = 0;
            int totalTimeSpent = 0;
            int qtdPendencias = 0;

            XmlDocument xml = new XmlDocument();
            xml.Load(vpEnderecoXML);
            XmlElement Elem = xml.DocumentElement;

            for (int i = 0; i < Elem.ChildNodes.Item(0).ChildNodes.Count; i++)
            {
                if (Elem.ChildNodes.Item(0).ChildNodes.Item(i).Name == "item")
                {
                    qtdPendencias++;
                    for (int j = 0; j < Elem.ChildNodes.Item(0).ChildNodes.Item(i).ChildNodes.Count; j++)
                    {
                        if (Elem.ChildNodes.Item(0).ChildNodes.Item(i).ChildNodes.Item(j).Name == "timeestimate ")
                        {
                            //channel>item>timeestimate>seconds
                            totalTimeEstimate = totalTimeEstimate + Convert.ToInt32(Elem.ChildNodes.Item(0).ChildNodes.Item(i).ChildNodes.Item(j).Attributes.Item(0).Value);
                        }
                        else
                            if (Elem.ChildNodes.Item(0).ChildNodes.Item(i).ChildNodes.Item(j).Name == "timespent")
                            {
                                //channel>item>timespent>seconds
                                totalTimeSpent = totalTimeSpent + Convert.ToInt32(Elem.ChildNodes.Item(0).ChildNodes.Item(i).ChildNodes.Item(j).Attributes.Item(0).Value);
                            }
                    }
                }

            }

            int[] result = new int[3] { qtdPendencias, totalTimeEstimate, totalTimeSpent };

            return result;
        }

        protected void btnGerarRelatorio_Click(object sender, EventArgs e)
        {
            string jira = "http://jira.sednasoft.com.br:8080/sr/jira.issueviews:searchrequest-xml/temp/SearchRequest.xml?jqlQuery=";
            string project = "project+%3D+" + ddlFiltroProjeto.SelectedValue;
            string sprint = "fixVersion%3D" + ddlFiltroSprint.SelectedValue;
            string url = jira + project + "+AND+issuetype+%3D+Revis%C3%A3o+and+cf%5B10292%5D+%3D+%22Teste-Sedna%22+and+" + sprint + "&tempMax=1&field=key&field=timeestimate&field=timespent";

            url = "http://jira.sednasoft.com.br:8080/sr/jira.issueviews:searchrequest-xml/temp/SearchRequest.xml?jqlQuery=project+%3D+TUB+AND+issuetype+%3D+Revis%C3%A3o+and+cf%5B10292%5D+%3D+%22Teste-Sedna%22+and+fixVersion%3DPA12Q2_01&tempMax=2&field=key&field=timeestimate&field=timespent";

            int[] valores = new int[3];
            
            valores = totaliza(Server.MapPath("SearchRequest.xml"));

            lblTituloRelatorio.Text = ddlFiltroProjeto.SelectedItem + ": " + ddlFiltroSprint.SelectedValue;

            lblAtivPTeAt.Text = Convert.ToString(segundosToHoras(valores[1]));
            lblAtivPTgAt.Text = Convert.ToString(segundosToHoras(valores[2]));
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }
    }
}
